import { Service , Inject } from "typedi";
import { UserEntity } from "../entity/UserEntity";
import { utils } from "../utils/utils";
import { UserInfoEntity } from "../entity/UserInfoEntity";
import { getRepository,getConnection } from "typeorm";


@Service()
export class UserDao{
    public async isUserDao(user_Phone:string): Promise<any> {

        return await     getRepository(UserEntity)
                            .createQueryBuilder("UserEntity")
                            .where("UserEntity.user_Phone = :user_Phone", { user_Phone: user_Phone })
                            .getOne();
    }
    public async updateUserData(params:{user_NickName:string,user_Id:number,user_Phone:string,user_PassWord:string,user_Email:string,user_HeadImg:string,user_AutoGraph:string}): Promise<any> {

         await     getConnection()
        .createQueryBuilder()
        .update(UserEntity)
        .where("user_Id = :id", { id: params.user_Id })
        .set({
            user_PassWord:params.user_PassWord,
            user_NickName:params.user_NickName,
            user_Phone:params.user_Phone,
            user_HeadImg:'dynamic/'+params.user_HeadImg
            })
        .execute();
        return await     getConnection()
        .createQueryBuilder()
        .update(UserInfoEntity)
        .where("userInfo_Id = :id", { id: params.user_Id })
        .set({
            user_Email:params.user_Email,
            user_Autograph:params.user_AutoGraph
            })
        .execute();
    }
    
    public async userLoginDao(user_Phone:string): Promise<any> {
        return await     getConnection()
                            .createQueryBuilder()
                            .update(UserEntity)
                            .where("user_Phone = :user_Phone", { user_Phone: user_Phone })
                            .set({ user_LoginStatus: 1 })
                            .execute();
    }

    public async setOneUser(params:{user_Phone:string,user_PassWord:string}){
       
        await getConnection()
        .createQueryBuilder()
        .insert()
        .into(UserEntity)
        .values({
            user_Phone:params.user_Phone,
            user_PassWord:params.user_PassWord,
            user_HeadImg:'userImg/imgHead.jpg',
            user_NickName:'暂无'
       })
        .execute();

      let UserData = await this.isUserDao(params.user_Phone);

        await  getConnection()
        .createQueryBuilder()
        .update(UserEntity)
        .where("user_Id = :id", { id: UserData.user_Id })
        .set({ userUserInfoUserInfoId:UserData.user_Id})
        .execute();

       return await getConnection()
        .createQueryBuilder()
        .insert()
        .into(UserInfoEntity)
        .values({
            userInfo_Id:UserData.user_Id,
            user_CreateTime:new Date(),
            user_FansList:JSON.stringify([]),
            user_FollowList:JSON.stringify([]),
            user_DynamicLikeList:JSON.stringify([]),
            user_RecentVisitList:JSON.stringify([]),
            user_CollectionList:JSON.stringify([]),
            user_LikeList:JSON.stringify([])
       })
        .execute();

    }

    public async getUserInfoDao(user_Id:number){

        let userData:UserEntity|undefined =  await getRepository(UserEntity)
                        .createQueryBuilder('UserEntity')
                        .where("UserEntity.user_Id = :user_Id", { user_Id: user_Id })
                        .leftJoinAndSelect('UserEntity.user_UserInfo',"UserInfoEntity")
                        .getOne()
                      
                        return userData;
    }

    public async setLikeListDao(user_Id:number,user_LikeList:string){

        return await getConnection()
        .createQueryBuilder()
        .update(UserInfoEntity)
        .where("userInfo_Id = :id", { id: user_Id })
        .set({ user_LikeList:user_LikeList})
        .execute();
    }
    public async setCollectionListDao(user_Id:number,user_CollectionList:string){

        return await getConnection()
        .createQueryBuilder()
        .update(UserInfoEntity)
        .where("userInfo_Id = :id", { id: user_Id })
        .set({ user_CollectionList})
        .execute();
    }
    
    public async setSubListDao(user_Id:number,user_SubList:string){

        return await getConnection()
        .createQueryBuilder()
        .update(UserInfoEntity)
        .where("userInfo_Id = :id", { id: user_Id })
        .set({ user_SubList:user_SubList})
        .execute();
    }

    public async setDynamicLikeListDao(user_Id:number,user_DynamicLikeList:string){

        return await getConnection()
        .createQueryBuilder()
        .update(UserInfoEntity)
        .where("userInfo_Id = :id", { id: user_Id })
        .set({ user_DynamicLikeList:user_DynamicLikeList})
        .execute();
    }
    public async setDynamicCollectionListDao(user_Id:number,user_DynamicCollectionList:string){

        return await getConnection()
        .createQueryBuilder()
        .update(UserInfoEntity)
        .where("userInfo_Id = :id", { id: user_Id })
        .set({ user_DynamicCollectionList:user_DynamicCollectionList})
        .execute();
    }
    
    public async setFollowListDao(user_Id:number,user_FollowList:string){

        return await getConnection()
        .createQueryBuilder()
        .update(UserInfoEntity)
        .where("userInfo_Id = :id", { id: user_Id })
        .set({ user_FollowList:user_FollowList})
        .execute();
    }


    public async setFansListDao(user_Id:number,user_FansList:string){

        return await getConnection()
        .createQueryBuilder()
        .update(UserInfoEntity)
        .where("userInfo_Id = :id", { id: user_Id })
        .set({ user_FansList:user_FansList})
        .execute();
    }

    public async setRecentVisitDao(user_Id:number,user_RecentVisitList:string){

        return await getConnection()
        .createQueryBuilder()
        .update(UserInfoEntity)
        .where("userInfo_Id = :id", { id: user_Id })
        .set({ user_RecentVisitList:user_RecentVisitList})
        .execute();
    }
    public async setRecentVisitUserDao(user_Id:number,user_RecentVisitUserList:string){

        return await getConnection()
        .createQueryBuilder()
        .update(UserInfoEntity)
        .where("userInfo_Id = :id", { id: user_Id })
        .set({ user_RecentVisitUserList:user_RecentVisitUserList})
        .execute();
    }
    public async setRecentVisitDynamicDao(user_Id:number,user_RecentVisitDynamicList:string){

        return await getConnection()
        .createQueryBuilder()
        .update(UserInfoEntity)
        .where("userInfo_Id = :id", { id: user_Id })
        .set({ user_RecentVisitDynamicList:user_RecentVisitDynamicList})
        .execute();
    }

    public async changeFollowUser(follow_Id:number,isFollow:string): Promise<any> {
        return await   getConnection()
        .createQueryBuilder()
        .update(UserEntity)
        .where(`user_Id = :id`, { id: follow_Id })
        .set({ user_Fans:()=>`user_Fans  ${isFollow == 'true'?'+':'-'} 1`})
        .execute();
    }

    public async changeFanUser(user_Id:number,isFollow:string): Promise<any> {
        return await   getConnection()
        .createQueryBuilder()
        .update(UserEntity)
        .where(`user_Id = :id`, { id: user_Id })
        .set({ user_Follows:()=>`user_Follows  ${isFollow == 'true'?'+':'-'} 1`})
        .execute();
    }

    public async changeDynamicNum(user_Id:number,isAdd:string): Promise<any> {
        return await   getConnection()
        .createQueryBuilder()
        .update(UserInfoEntity)
        .where(`userInfo_Id = :id`, { id: user_Id })
        .set({ user_DynamicNum:()=>`user_DynamicNum  ${isAdd == 'true'?'+':'-'} 1`})
        .execute();
    }
    
  

}